package edu.whu.service;

import edu.whu.entity.Product;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ProductService {
    private Map<Long, Product> productsMap = Collections.synchronizedMap(new HashMap<Long, Product>());

    public Product addProduct(Product product){
        productsMap.put(product.getId(), product);
        return product;
    }

    public Product getProduct(long id){
        return productsMap.get(id);
    }

    public List<Product> findProduct(String name, Double price){
        List<Product> result = new ArrayList<>();

        for(Product product: productsMap.values()){
            if(name != null && !product.getName().contains(name)) {
                continue;
            }
            if(price != null && !price.equals(product.getPrice())){

            }
            result.add(product);
        }
        return result;
    }

    public void updateProduct(long id, Product product) throws NullPointerException {
        Product oldProduct = productsMap.get(id);
        oldProduct.setName(product.getName());
        oldProduct.setPrice(product.getPrice());
        productsMap.put(id, oldProduct);
    }

    public void deleteProduct(long id){
        productsMap.remove(id);
    }
}
